package com.yf.controller;

import com.yf.bean.BaseResponse;
import com.yf.bean.CommentBean;
import com.yf.service.CommentService;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Log4j2
@RestController
@RequestMapping(value = "${custom.api-version}")
public class CommentController {
    @Autowired
    CommentService commentService;

    @PostMapping("/comment")
    public BaseResponse<Integer> addComment(@RequestBody CommentBean bean) {
        if (bean == null) {
            return BaseResponse.success();
        }
        int ret = commentService.addComment(bean);
        if (ret > 0) {
            return BaseResponse.success(bean.getId());
        }
        return BaseResponse.fail();
    }

    @PostMapping("/reply")
    public BaseResponse<Integer> addReply(@RequestBody CommentBean bean) {
        if (bean == null) {
            return BaseResponse.success();
        }
        log.info(bean);
        int ret = commentService.addReply(bean);
        if (ret > 0) {
            return BaseResponse.success(bean.getId());
        }
        return BaseResponse.fail();
    }

    @GetMapping("/comment")
    public BaseResponse<List<CommentBean>> getCommentByPid(
            @RequestParam(name = "pid", defaultValue = "0") Integer pid,
            @RequestParam(name = "offset", defaultValue = "0") Integer offset,
            @RequestParam(name = "count", defaultValue = "10") Integer count
    ) {
        if (pid.equals(0)) {
            return BaseResponse.success();
        }
        if (offset < 0) {
            offset = 0;
        }
        if (count < 0) {
            count = 10;
        }
        Map<String, Object> params = new HashMap<>(3);
        params.put("pid", pid);
        params.put("offset", offset);
        params.put("count", count);
        List<CommentBean> comments = commentService.getCommentByPid(params);
        return BaseResponse.success(comments);
    }

    @GetMapping("/conversation")
    public BaseResponse<List<CommentBean>> getCommentByCid(
            @RequestParam(name = "cid", defaultValue = "0") Integer cid,
            @RequestParam(name = "offset", defaultValue = "0") Integer offset,
            @RequestParam(name = "count", defaultValue = "10") Integer count
    ) {
        if (cid.equals(0)) {
            return BaseResponse.success();
        }
        if (offset < 0) {
            offset = 0;
        }
        if (count < 0) {
            count = 10;
        }
        Map<String, Object> params = new HashMap<>(3);
        params.put("cid", cid);
        params.put("offset", offset);
        params.put("count", count);
        List<CommentBean> comments = commentService.getCommentByCid(params);
        return BaseResponse.success(comments);
    }

    @PostMapping("/comment/delete")
    public BaseResponse<Boolean> deleteComment(
            @RequestParam(name = "id", defaultValue = "0") Integer id) {
        if (id <= 0) {
            return BaseResponse.fail();
        }
        int i = commentService.deleteComment(id);
        return BaseResponse.success(i > 0);
    }

    @GetMapping("/comment/like")
    public BaseResponse<Boolean> addLike(
            @RequestParam(name = "id", defaultValue = "0") Integer id,
            @RequestParam(name = "op", defaultValue = "0") Integer op) {
        if (id.equals(0)) {
            return BaseResponse.fail();
        }
        final boolean ret = commentService.opLike(id, op);
        return BaseResponse.success(ret);
    }

    @GetMapping("/comment/dislike")
    public BaseResponse<Boolean> addDislike(
            @RequestParam(name = "id", defaultValue = "0") Integer id,
            @RequestParam(name = "op", defaultValue = "0") Integer op) {
        if (id.equals(0)) {
            return BaseResponse.fail();
        }
        final boolean ret = commentService.opDislike(id, op);
        return BaseResponse.success(ret);
    }

    @GetMapping("/comment/num")
    public BaseResponse<Integer> getUserUnreadComment() {
        return BaseResponse.success(commentService.getUserUnreadMsg());
    }
}
